import 'package:flutter/material.dart';
import 'package:awak/common/values/images.dart';

class AwakToast {
  static OverlayEntry? _currentToast;

  static void showCustomToast(BuildContext context, String message, {Duration duration = const Duration(seconds: 2)}) {
    _currentToast?.remove();
    _currentToast = null;

    final overlay = Overlay.of(context);

    late OverlayEntry entry;
    entry = OverlayEntry(
      builder: (context) => _ToastWithFade(
        message: message,
        duration: duration,
        onDismiss: () {
          entry.remove();
          if (_currentToast == entry) _currentToast = null;
        },
      ),
    );
    overlay.insert(entry);
    _currentToast = entry;
  }
}

class _ToastWithFade extends StatefulWidget {
  final String message;
  final Duration duration;
  final VoidCallback onDismiss;
  const _ToastWithFade({required this.message, required this.duration, required this.onDismiss});

  @override
  State<_ToastWithFade> createState() => _ToastWithFadeState();
}

class _ToastWithFadeState extends State<_ToastWithFade> with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  late Animation<double> _opacity;
  bool _isDisposed = false;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 250),
      reverseDuration: const Duration(milliseconds: 250),
    );
    _opacity = CurvedAnimation(parent: _controller, curve: Curves.easeInOut);
    _controller.forward();
    Future.delayed(widget.duration, () async {
      if (mounted && !_isDisposed) {
        await _controller.reverse();
        if (mounted) {
          widget.onDismiss();
        }
      }
    });
  }

  @override
  void dispose() {
    _isDisposed = true;
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FadeTransition(
      opacity: _opacity,
      child: Stack(
        children: [
          Positioned.fill(
            child: IgnorePointer(
              child: Container(
                color: Colors.black.withOpacity(0.65),
              ),
            ),
          ),
          Center(
            child: Material(
              color: Colors.transparent,
              child: _CustomToastWidget(message: widget.message),
            ),
          ),
        ],
      ),
    );
  }
}

class _CustomToastWidget extends StatelessWidget {
  final String message;
  const _CustomToastWidget({Key? key, required this.message}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      clipBehavior: Clip.none,
      alignment: Alignment.topCenter,
      children: [
        Container(
          margin: const EdgeInsets.only(top: 32),
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 24),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(16),
            gradient: const LinearGradient(
              colors: [Color(0xFFD3F427), Color(0xFFFFFFFF)],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
            ),
            boxShadow: [
              BoxShadow(
                color: Colors.black.withOpacity(0.08),
                blurRadius: 12,
                offset: Offset(0, 4),
              ),
            ],
          ),
          child: Text(
            message,
            style: const TextStyle(
              color: Color(0xFF262626),
              fontSize: 16,
              fontWeight: FontWeight.w600,
            ),
            textAlign: TextAlign.center,
          ),
        ),
        Positioned(
          top: 0,
          child: Container(
            height: 56,
            child: Image.asset(
              AwakImages.logo2,
              fit: BoxFit.cover,
            ),
          ),
        ),
      ],
    );
  }
}
